package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.AgreementEntity;
import org.jeecg.modules.system.mapper.AgreementMapper;
import org.jeecg.modules.system.service.AgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AgreementServiceImpl implements AgreementService {


    @Autowired
    AgreementMapper mapper;
    /**
     * 查询所有协议
     * @param page
     * @param title
     * @param type
     * @return
     */
    @Override
    public IPage<AgreementEntity> getAgreementList(Page<AgreementEntity> page, String title, int type) {

        IPage<AgreementEntity> pageList=mapper.getAgreementList(page,title,type);

        if(pageList!=null){
            for(int i=0;i<pageList.getRecords().size();i++){
                String pingleiname=mapper.getpingleinamebyId(pageList.getRecords().get(i).getCategoryId());
                if(pingleiname==null){
                    pageList.getRecords().get(i).setCategoryName("暂无");
                }else {
                    pageList.getRecords().get(i).setCategoryName(pingleiname);
                }
            }
        }
        return pageList;
    }

    /**
     * 新增协议
     * @param entity
     * @return
     */
    @Override
    public int addAgreement(AgreementEntity entity) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        entity.setSysOrgCode(user.getOrgCode());
        entity.setUpdateBy(user.getRealname());
        entity.setUpdateTime(new Date());
        return mapper.addAgreement(entity);
    }

    /**
     * 修改数据状态，发布，停用，删除通用
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updatestatus(int id, int status) {
        return mapper.updatestatus(id,status);
    }

    /**
     * 通过id查询协议详情
     * @param id
     * @return
     */
    @Override
    public AgreementEntity getAgreementEntitybyId(int id) {
        return mapper.getAgreementEntitybyId(id);
    }

    /**
     * 修改协议
     * @param entity
     * @return
     */
    @Override
    public int updateAgreement(AgreementEntity entity) {
        entity.setUpdateTime(new Date());
        System.out.println(entity.toString());
        return mapper.updateAgreement(entity);
    }

    /**
     * 查询是否已经存在
     * @param type
     * @return
     */
    @Override
    public String getxieyicunzai(int type) {
        return mapper.getxieyicunzai(type);
    }


    /**
     * 查询这个品类的参议表是否已经存在
     * @param categoryId
     * @return
     */
    @Override
    public AgreementEntity getcanzhaobycateId(String categoryId) {
        return mapper.getcanzhaobycateId(categoryId);
    }
}
